/**
 * Created by zhaoxb on 2017/4/18.
 * utils:工具类
 */
/**==========================================================**/

function isType(obj,type){
    return Object.prototype.toString.call(obj) === "[object " + type + "]";
}

var SIGN_REGEXP = /([yMdhsm])(\1*)/g;
var DEFAULT_PATTERN = 'yyyy-MM-dd';

function padding(s, len) {
    var len = len - (s + '').length;
    for (var i = 0; i < len; i++) { s = '0' + s; }
    return s;
};

export default {
    Class:function (){
        var len = arguments.length;
        var P = arguments[0];
        var F = arguments[len-1];

        var C = typeof F.initialize == "function" ?
            F.initialize :
            function(){ P.prototype.initialize.apply(this, arguments); };

        if (len > 1) {
            var newArgs = [C, P].concat(
                Array.prototype.slice.call(arguments).slice(1, len-1), F);
            this.inherit.apply(null, newArgs);
        } else {
            C.prototype = F;
        }
        return C;
    },
    inherit:function(C,P){
        var F = function() {};
        F.prototype = P.prototype;
        C.prototype = new F();
        C.prototype.super = P;
        C.prototype.constructor = C;
        var i, l, o;
        for(i=2, l=arguments.length; i<l; i++) {
            o = arguments[i];
            if(typeof o === "function") {
                o = o.prototype;
            }
            Utils.extend(C.prototype, o);
        }
    },
    extend:function(){
        var options, name, src, copy, copyIsArray, clone;
        var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false;

        // Handle a deep copy situation
        if (this.isBool(target)) {
            deep = target;
            target = arguments[i] || {};
            i++;
        }
        // Handle case when target is a string or something (possible in deep copy)
        if (typeof target !== 'object' && !this.isFunction(target)) {
            target = {};
        }
        // Extend Util itself if only one argument is passed
        if (i === length) {
            target = this;
            i--;
        }
        for (; i < length; i++) {
            // Only deal with non-null/undefined values
            if ((options = arguments[i]) != null) {
                // Extend the base object
                for (name in options) {
                    src = target[name];
                    copy = options[name];

                    // Prevent never-ending loop
                    if (target === copy) {
                        continue;
                    }

                    // Recurse if we're merging plain objects or arrays
                    if (deep && copy && (this.isObject(copy) || (copyIsArray = this.isArray(copy)))) {
                        if (copyIsArray) {
                            copyIsArray = false;
                            clone = src && this.isArray(src) ? src : [];

                        } else {
                            clone = src && this.isObject(src) ? src : {};
                        }

                        // Never move original objects, clone them
                        target[name] = this.extend(deep, clone, copy);
                    }
                    // Don't bring in undefined values
                    else if (copy !== undefined) {
                        target[name] = copy;
                    }
                }
            }
        }
        // Return the modified object
        return target;
    },
    isBool:function(bool){
        return typeof bool === 'boolean';
    },
    isObject:function(obj){
        return isType(obj,"Object");
    },
    isArray:function(obj){
        return Array.isArray(obj) || isType(obj,"Array");
    },
    isString:function(obj){
        return isType(obj,"String");
    },
    isFunction:function(obj){
        return isType(obj,"Function");
    },
    isHTMLElement:function(obj){
        try{
            return obj.nodeType == 1?true:false;
        }catch(e){
            return false;
        }            
    },
    each:function(iterator, callback, context){
        var i, ret;
        if (!context) {
            context = this;
        }
        // 数组
        if (this.isArray(iterator)) {
            for (i = 0; i < iterator.length; i++) {
                ret = callback.call(context, iterator[i], i, iterator);
                // 回调返回 false 退出循环
                if (ret === false) {
                    break;
                }
                // 回调返回 null 从原数组删除当前选项
                if (ret === null) {
                    iterator.splice(i, 1);
                    i--;
                }
            }

        } else if (this.isObject(iterator)) {
            var keys = Object.keys(iterator);
            for (i = 0; i < keys.length; i++) {
                var key = keys[i];
                ret = callback.call(context, iterator[key], key, iterator);

                // 回调返回 false 退出循环
                if (ret === false) {
                    break;
                }
                // 回调返回 null 从原对象删除当前选项
                if (ret === null) {
                    delete iterator[key];
                }
            }
        }
    },

    getQueryStringByName: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        var context = "";
        if (r != null)
            context = r[2];
        reg = null;
        r = null;
        return context == null || context == "" || context == "undefined" ? "" : context;
    },
    formatDate: {


        format: function (date, pattern) {
            pattern = pattern || DEFAULT_PATTERN;
            return pattern.replace(SIGN_REGEXP, function ($0) {
                switch ($0.charAt(0)) {
                    case 'y': return padding(date.getFullYear(), $0.length);
                    case 'M': return padding(date.getMonth() + 1, $0.length);
                    case 'd': return padding(date.getDate(), $0.length);
                    case 'w': return date.getDay() + 1;
                    case 'h': return padding(date.getHours(), $0.length);
                    case 'm': return padding(date.getMinutes(), $0.length);
                    case 's': return padding(date.getSeconds(), $0.length);
                }
            });
        },
        parse: function (dateString, pattern) {
            var matchs1 = pattern.match(SIGN_REGEXP);
            var matchs2 = dateString.match(/(\d)+/g);
            if (matchs1.length == matchs2.length) {
                var _date = new Date(1970, 0, 1);
                for (var i = 0; i < matchs1.length; i++) {
                    var _int = parseInt(matchs2[i]);
                    var sign = matchs1[i];
                    switch (sign.charAt(0)) {
                        case 'y': _date.setFullYear(_int); break;
                        case 'M': _date.setMonth(_int - 1); break;
                        case 'd': _date.setDate(_int); break;
                        case 'h': _date.setHours(_int); break;
                        case 'm': _date.setMinutes(_int); break;
                        case 's': _date.setSeconds(_int); break;
                    }
                }
                return _date;
            }
            return null;
        }

    }
}


